package cc.mrbird.lsSubmission.service.impl;

import cc.mrbird.lsSubmission.dao.LsSubmissionMapper;
import cc.mrbird.lsSubmission.domain.LsPullDown;
import cc.mrbird.lsSubmission.domain.LsSubmissionEntry;
import cc.mrbird.lsSubmission.service.LsSubmissionService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LsSubmissionServiceImpl implements LsSubmissionService {
    @Autowired
    private LsSubmissionMapper lsSubmissionMapper;

    @Override
    public void saveLsSubmissionEntry(LsSubmissionEntry lsSubmissionEntry) {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(d);
        String processingResults = lsSubmissionEntry.getProcessingResults();
        if(processingResults == null){
            lsSubmissionEntry.setProcessingResults("");
        }
        String complaintType = lsSubmissionEntry.getComplaintType();
        if(complaintType == null){
            lsSubmissionEntry.setComplaintType("");
        }
        String dutyDetermine = lsSubmissionEntry.getDutyDetermine();
        if(dutyDetermine == null){
            lsSubmissionEntry.setDutyDetermine("");
        }
        String handler = lsSubmissionEntry.getHandler();
        if(handler == null){
            lsSubmissionEntry.setHandler("");
        }
        String processing = lsSubmissionEntry.getProcessing();
        System.out.println(processing);

        if(processing == null){
            lsSubmissionEntry.setProcessing("26");
        }
        if(processing == "25"){
            lsSubmissionEntry.setProcessingTime(str);
        }

        lsSubmissionMapper.saveLsSubmissionEntry(lsSubmissionEntry);

    }

    @Override
    public List<LsSubmissionEntry> getLsSubmissionEntryList(LsSubmissionEntry lsSubmissionEntry) {

        String handler  = lsSubmissionEntry.getHandler();
        if(handler.equals("")){
            lsSubmissionEntry.setHandler(null);
        }

        String startTime  = lsSubmissionEntry.getStartTime();
        if(startTime.equals("")){
            lsSubmissionEntry.setStartTime(null);
        }
        String endTime  = lsSubmissionEntry.getEndTime();
        if(endTime.equals("")){
            lsSubmissionEntry.setEndTime(null);
        }
        String startTime1  = lsSubmissionEntry.getStartTime1();
        if(startTime1.equals("")){
            lsSubmissionEntry.setStartTime1(null);
        }
        String endTime1  = lsSubmissionEntry.getEndTime1();
        if(endTime1.equals("")){
            lsSubmissionEntry.setEndTime1(null);
        }
        String id  = lsSubmissionEntry.getId();
        if(id.equals("")){
            lsSubmissionEntry.setId(null);
        }
/*        String busCompany  = submissionEntry.getBusCompany();
        if(busCompany.equals("")){
            submissionEntry.setBusCompany(null);
        }*/
        String paymentChannels  = lsSubmissionEntry.getPaymentChannels();
        if(paymentChannels.equals("")){
            lsSubmissionEntry.setPaymentChannels(null);
        }
        String complaintType  = lsSubmissionEntry.getComplaintType();
        if(complaintType.equals("")){
            lsSubmissionEntry.setComplaintType(null);
        }
        String processing  = lsSubmissionEntry.getProcessing();
        if(processing.equals("")){
            lsSubmissionEntry.setProcessing(null);
        }
        String complaintChannels  = lsSubmissionEntry.getComplaintChannels();
        if(complaintChannels.equals("")){
            lsSubmissionEntry.setComplaintChannels(null);
        }
        String dutyDetermine  = lsSubmissionEntry.getDutyDetermine();
        if(dutyDetermine.equals("")){
            lsSubmissionEntry.setDutyDetermine(null);
        }
        String contactInformation  = lsSubmissionEntry.getContactInformation();
        if(contactInformation.equals("")){
            lsSubmissionEntry.setContactInformation(null);
        }
        List<LsSubmissionEntry> lsSubmission = lsSubmissionMapper.getLsSubmissionEntryList(lsSubmissionEntry);
        return lsSubmission;
    }

    @Override
    public LsSubmissionEntry getByLsSubmissionId(LsSubmissionEntry lsSubmissionEntry) {
        List<LsSubmissionEntry> list = lsSubmissionMapper.getLsSubmissionEntryList(lsSubmissionEntry);
        if (list == null || list.size()==0){
            return null;
        }
        for(LsSubmissionEntry LsSubmission:list){
            String processingResults = LsSubmission.getProcessingResults();
            if(processingResults == null){
                LsSubmission.setProcessingResults("");
            }
            String complaintType = LsSubmission.getComplaintType();
            if(complaintType == null){
                LsSubmission.setComplaintType("");
            }
            String dutyDetermine = LsSubmission.getDutyDetermine();
            if(dutyDetermine == null){
                LsSubmission.setDutyDetermine("");
            }
            String handler = LsSubmission.getHandler();
            if(handler == null){
                LsSubmission.setHandler("");
            }
            String processing = LsSubmission.getProcessing();
            if(processing == null){
                LsSubmission.setProcessing("");
            }
            String paymentChannels = LsSubmission.getPaymentChannels();
            if(paymentChannels == null){
                LsSubmission.setPaymentChannels("");
            }
            String complaintChannels = LsSubmission.getComplaintChannels();
            if(complaintChannels == null){
                LsSubmission.setComplaintChannels("");
            }
            String complaintLine = LsSubmission.getComplaintLine();
            if(complaintLine == null){
                LsSubmission.setComplaintLine("");
            }
            String contactInformation = LsSubmission.getContactInformation();
            if(contactInformation == null){
                LsSubmission.setContactInformation("");
            }
            String registrar = LsSubmission.getRegistrar();
            if(registrar == null){
                LsSubmission.setRegistrar("");
            }
            String remakes = LsSubmission.getRemakes();
            if(remakes == null){
                LsSubmission.setRemakes("");
            }
            String contentsComplaints = LsSubmission.getContentsComplaints();
            if(contentsComplaints == null){
                LsSubmission.setContentsComplaints("");
            }
            String pictureName = LsSubmission.getPicName();
            if(pictureName != null){
//                String str = pictureName.substring(4);
//                String picName = "http://tuika.tianyaxing.com/"+str;
                //String picName = "http://127.0.0.1:8888/"+str;
                LsSubmission.setPicName("images/"+pictureName);
            }
        }
        return list.get(0);
    }

    @Override
    public void updateLsSubmission(LsSubmissionEntry lsSubmissionEntry) {
        lsSubmissionMapper.updateLsSubmission(lsSubmissionEntry);
    }

    @Override
    public List<LsPullDown> FindLsPullDown(LsPullDown lsPullDown) {
        return lsSubmissionMapper.FindLsPullDown(lsPullDown);
    }

    @Override
    public void addLsPullDown(LsPullDown lsPullDown) {
        lsSubmissionMapper.addLsPullDown(lsPullDown);
    }

    @Override
    public void cancelLsSubmission(LsSubmissionEntry lsSubmissionEntry) {
        lsSubmissionMapper.cancelLsSubmission(lsSubmissionEntry);
    }
    @Override
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public boolean lsBatchImport(String fileName, MultipartFile file) throws Exception {

        boolean notNull = false;
        List<LsSubmissionEntry> lsSubmissionEntryList = new ArrayList<LsSubmissionEntry>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new Exception("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if(sheet!=null){
            notNull = true;
        }
        LsSubmissionEntry lsSubmissionEntry;
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }

            lsSubmissionEntry = new LsSubmissionEntry();
            row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            String acceptanceTime = row.getCell(0).getStringCellValue();
            if(acceptanceTime == null || acceptanceTime.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,受理时间未填写)");
            }
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            String paymentChannels = row.getCell(1).getStringCellValue();
            if(paymentChannels == null || paymentChannels.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,受理支付渠道未填写)");
            }
            row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
            String complaintChannels = row.getCell(2).getStringCellValue();
            if(complaintChannels == null || complaintChannels.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,投诉渠道未填写)");
            }
            row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
            String complaintType = row.getCell(3).getStringCellValue();
            if(complaintType == null || complaintType.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,投诉类别未填写)");
            }

            row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
            String contactInformation = row.getCell(4).getStringCellValue();
            if(contactInformation == null || contactInformation.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,联系方式未填写)");
            }
            row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
            String complaintLine = row.getCell(5).getStringCellValue();
            if(complaintLine == null || complaintLine.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,投诉线路未填写)");
            }
            row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
            String contentsComplaints = row.getCell(6).getStringCellValue();
            if(contentsComplaints == null || contentsComplaints.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,投诉内容未填写)");
            }
            row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
            String dutyDetermine = row.getCell(7).getStringCellValue();
            if(dutyDetermine == null || dutyDetermine.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,责任判定未填写)");
            }
            row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
            String processingResults = row.getCell(8).getStringCellValue();
            if(processingResults == null || processingResults.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,处理结果未填写)");
            }
            row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
            String registrar = row.getCell(9).getStringCellValue();
            if(registrar == null || registrar.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,登记人未填写)");
            }
            row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
            String processingTime = row.getCell(10).getStringCellValue();
            if(processingTime == null || processingTime.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,处理时间未填写)");
            }
            row.getCell(11).setCellType(Cell.CELL_TYPE_STRING);
            String handler = row.getCell(11).getStringCellValue();
            if(handler == null || handler.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,处理人未填写)");
            }
            row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
            String busCompany = row.getCell(12).getStringCellValue();
            if(busCompany == null || busCompany.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,公交公司未填写)");
            }
            row.getCell(13).setCellType(Cell.CELL_TYPE_STRING);
            String processing = row.getCell(13).getStringCellValue();
            if(processing == null || processing.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,处理状态未填写)");
            }
            row.getCell(14).setCellType(Cell.CELL_TYPE_STRING);
            String city = row.getCell(14).getStringCellValue();
            if(city == null || city.isEmpty()){
                throw new Exception("导入失败(第"+(r+1)+"行,处理状态未填写)");
            }
            if(acceptanceTime ==null || paymentChannels ==null || complaintType==null || contactInformation==null
                    || complaintLine ==null || contentsComplaints ==null || dutyDetermine==null || processingResults==null
                    || registrar ==null || handler ==null || busCompany==null || complaintChannels==null ||processing==null||city==null||processingTime==null){
                continue;
            }

            lsSubmissionEntry.setAcceptanceTime(acceptanceTime);
            lsSubmissionEntry.setPaymentChannels(paymentChannels);
            lsSubmissionEntry.setComplaintType(complaintType);
            lsSubmissionEntry.setContactInformation(contactInformation);
            lsSubmissionEntry.setComplaintLine(complaintLine);
            lsSubmissionEntry.setContentsComplaints(contentsComplaints);
            lsSubmissionEntry.setDutyDetermine(dutyDetermine);
            lsSubmissionEntry.setProcessingResults(processingResults);
            lsSubmissionEntry.setRegistrar(registrar);
            lsSubmissionEntry.setHandler(handler);
            lsSubmissionEntry.setBusCompany(busCompany);
            lsSubmissionEntry.setComplaintChannels(complaintChannels);
            lsSubmissionEntry.setProcessing(processing);
            lsSubmissionEntry.setCity(city);
            lsSubmissionEntry.setProcessingTime(processingTime);
            lsSubmissionEntryList.add(lsSubmissionEntry);
        }

        for (LsSubmissionEntry lsSubmissionEntryResord : lsSubmissionEntryList) {
            lsSubmissionMapper.importLsSubmissionEntry(lsSubmissionEntryResord);
            System.out.println(" 插入 "+lsSubmissionEntryResord);

        }
        return notNull;
    }

    @Override
    public LsPullDown saveLsPullDown(LsPullDown lsPullDown) {
        return lsSubmissionMapper.saveLsPullDown(lsPullDown);
    }

    @Override
    public List<LsSubmissionEntry> getLsSubmissionEntryExportExcel(LsSubmissionEntry lsSubmissionEntry) {
        List<LsSubmissionEntry> lsSubmission = lsSubmissionMapper.getLsSubmissionEntryExportExcel(lsSubmissionEntry);
        return lsSubmission;
    }

    @Override
    public String findId() {
        return lsSubmissionMapper.findId();
    }

    @Override
    public LsSubmissionEntry getLsSubmissionEntry(LsSubmissionEntry lsSubmissionEntry) {
        return lsSubmissionMapper.getLsSubmissionEntry(lsSubmissionEntry);
    }

    @Override
    public String sumFirst() {
        return lsSubmissionMapper.sumFirst();
    }

    @Override
    public String sumFirst1() {
        return lsSubmissionMapper.sumFirst1();
    }

}
